<?php
/**
* @filename source_module.php 2013-11-11 02:10:56x
* @author Huming Xu <huming17@126.com>
* @version 1.0.0
* @copyright DZF (c) 2013, Huming Xu
*/

class ctrl_coupon_use extends coupon_use{
    public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
    
    static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
        
    /*
	*	取出一条信息
	*/
	public function do_detail($cu_id='') {
		$detail = array();
        $cu_id = isset($_REQUEST['cu_id']) ? $_REQUEST['cu_id'] : '';
		$detail = $this->one_info('coupon_use','cu_id',$cu_id);
        //DEBUG 调试输出
        include template('coupon/use/detail');
	}
    
	/*
	*  添加系统信息
	*/
	public function do_add() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$coupon_use = isset($_REQUEST['coupon_use']) && !empty($_REQUEST['coupon_use']) ? $_REQUEST['coupon_use']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验 根据具体业务逻辑添加
            //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
            foreach($coupon_use AS $key => $value){
                if(is_array($value) && !empty($value)){
                    $coupon_use[$key] = implode(",", $value);
                }
            }
            $coupon_use['create_dateline'] = TIMESTAMP;
            $insert_id = $this->add('coupon_use',$coupon_use);
            if($insert_id){
                showmessage('操作成功','index.php?mod=coupon&action=use&do=index');
            }else{
                showmessage('操作失败','index.php?mod=coupon&action=use&do=index');
            }
		}else{
			include template('coupon/use/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function do_edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$coupon_use = isset($_REQUEST['coupon_use']) && !empty($_REQUEST['coupon_use']) ? $_REQUEST['coupon_use']:'';
        $cu_id = isset($_REQUEST['cu_id']) ? $_REQUEST['cu_id'] : '';
        if($cu_id){
            if('1'==$issubmit){
                $coupon_use['modify_dateline'] = TIMESTAMP;
                //DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
                foreach($coupon_use AS $key => $value){
                    if(is_array($value) && !empty($value)){
                        $coupon_use[$key] = implode(",", $value);
                    }
                }                
                $where = array('cu_id'=>$cu_id);
                $effect_row = $this->edit('coupon_use',$coupon_use,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=coupon&action=use&do=index');
                }else{
                    showmessage('操作失败','index.php?mod=coupon&action=use&do=index');
                }
            }else{
                //DEBUG 获取操作对象信息
                $info = $this->one_info('coupon_use','cu_id',$cu_id);
                include template('coupon/use/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=coupon&action=use&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function do_delete() {
        global $_G;
        $cu_id = isset($_REQUEST['cu_id']) ? $_REQUEST['cu_id'] : '';
        if($cu_id){
            $effect_row = $this->delete("coupon_use", array("cu_id" => $cu_id), $limit=1);
            if($effect_row){
                showmessage('操作成功','index.php?mod=coupon&action=use&do=index');
            }else{
                showmessage('操作失败','index.php?mod=coupon&action=use&do=index');
            }
        }
    }
	/*
	*  系统信息列表
	*/
	public function do_index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql .= " AND  LIKE '%".$keyword."%' ";
        }
        $page_result=array();
        if($_G['user_id']){
            $wheresql .= " AND  cu_owner_user_id ='".$_G['user_id']."' AND cu_status=0 ";
            $orderby = " ORDER BY cu_id DESC ";
            //DEBUG 初始化请求请求获取一页列表数据的参数
            $page_condition=array(
                'page' => $page,//int 请求页面 页码
                'limit' => $limit,//int 每页请求个数
                'perpage' => $perpage,//int 每页显示个数
                'wheresql' => $wheresql,//string //条件SQL语句 
                'orderby' => $orderby,//string 排序规则
            );
            //DEBUG 列表数据返回结构
            /*
            $page_result = array(
                //int 返回结果总数
                'total_rows' => $total_rows,
                //array 一页数据数组
                'page_data' => $page_data 
            );
            */
            $page_result = $this->index('coupon_use',$page_condition);
            foreach($page_result['page_data'] AS $key => $value){
                //DEBUG  获取有没有过期、有效状态;
                $tmp_value=array();
                if($value['cc_id']){
                   $wheresql=" AND cc_status=1 AND cc_start_dateline < '".TIMESTAMP."' AND cc_end_dateline > '".TIMESTAMP."' ";
                   $tmp_value=$this->get_field_value_by_id('coupon_code','cc_id',$value['cc_id'],'cc_identifies,cc_cate_id,cc_title,cc_img_big,cc_img_small,cc_value,cc_num,cc_brife,cc_url',$wheresql);
                }
                if(!empty($tmp_value)){
                    $tmp_value["cu_id"]=$value['cu_id'];
                    $tmp_value["cc_code"]=$value['cc_code'];
                    $tmp_value["cc_img_big"]=$value['cc_img_big'];
                    $tmp_value["cc_img_small"]=$value['cc_img_small'];
                }
                $page_result['page_data'][$key]=$tmp_value;
            }
        }
        if($_G['gp_api']){
            $return['code'] = '0';
            $return['data'][$_G['gp_mod']][$_G['gp_action']][$_G['gp_do']]=$page_result;
            echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
        }else{
            $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=coupon&action=use&do=index&keyword=".$keyword);
            include template('coupon/use/index');	  
        }
	}

   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function do_api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $orderby = " ORDER BY cu_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition=array(
            'page' => $page,//int 请求页面 页码
            'limit' => $limit,//int 每页请求个数
            'perpage' => $perpage,//int 每页显示个数
            'wheresql' => $wheresql,//string //条件SQL语句 
            'orderby' => $orderby,//string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
        $page_result = array(
            //int 返回结果总数
            'total_rows' => $total_rows,
            //array 一页数据数组
            'page_data' => $page_data 
        );
        */
        $page_result = $this->api('coupon_use',$page_condition);      
        if('json'==$return_type){
            $return = json_encode($page_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
		echo $return;
	}
}
?>